package com.gvfish.backingbean;
import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class J_UrlReadData {

	public static void main(String args[])
	{
		try
		{
			URL url = new URL("http://cnfish.com/htm/news/20100106/8713.html");
			HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
			//urlcon.connect();
			BufferedReader r=new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
			
			String s = null;
			String temp=null;
			String type = null;
			Pattern p=Pattern.compile("charset\\s*=\\s*([^\"]+)");
			while((temp=r.readLine())!=null)
			{
				s = s + temp + "\t";
				Matcher m=p.matcher(new String(temp));
				if(m.find()){
					type = m.group();
					type = type.replace("charset=", "");
					System.out.println("页面格式为："+type);
					break;
				}
			}
			//type = "utf-8";
			r=new BufferedReader(new InputStreamReader(url.openStream(),type));
			while((temp=r.readLine())!=null){
				
				System.out.println(temp);
			}
			r.close();
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}
}
